﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contracts;
using System.ServiceModel;
using WcfContrib.Client;

namespace WcfContrib.Showcase.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //load runtime, if not called will be called automatically upon first access to the client channel
            MyServiceClient.LoadRuntime();

            //set the endpoint name, here we do it by setting the global definition - for all these client contract instances
            MyServiceClient.GlobalEndpointName = "ServiceEP";

            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Press any key to make a call to the service, 'Enter' to exit'");

                if (Console.ReadKey().Key == ConsoleKey.Enter)
                {
                    break;
                }

                try
                {
                    TestClientCall();

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Service was called successfully");
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Error: " + ex.ToString());
                }
            }

            //Dispose the client channel instance when exiting
            ClientChannel<IService>.Instance.Dispose();
        }

        static void TestClientCall()
        {
            //we call the service
            MyServiceClient.Instance.Do(new Foo { Id = 1, Name = "Some Foo" });
        }
    }
}
